<?php
/***
	客户联系人表，包括：
	1. 根据客户ID查询联系人 get_by_client($client_id)
	
**/
class Contact_person_model extends CI_Model {
	
	public function __construct() {
		$this->load->database();
	}
	
	//根据客户ID查询联系人
	public function get_by_client($client_id) {
		
		$sqlstring = "select * from contact_person where client_id = '".$client_id."' and name <> 'Others'";
		$query = $this->db->query($sqlstring);
		
		return $query->result_array();
	}
	
	//根据客户ID查询所有联系人
	public function get_by_client_all($client_id) {
		
		$sqlstring = "select * from contact_person where client_id = '".$client_id."'";
		$query = $this->db->query($sqlstring);
		
		return $query->result_array();
	}
	//根据ID查询联系人
	public function get_by_id($id) {
		
		$query = $this->db->get_where('contact_person', array('id' => $id));
		return $query->row_array();
	}
	
	//新增联系人
	public function create () {
		
		$data = array(
			'name' => $this->input->post('name'),
			'position' => $this->input->post('position'),
			'client_id' => $this->input->post('client_id'),
			'mobile' => $this->input->post('mobile'),
			'tel' => $this->input->post('tel'),
			'tax' => $this->input->post('tax'),
			'email' => $this->input->post('email')
		);
		
		return $this->db->insert('contact_person', $data);
	}
	
	//修改联系人
	public function update_all ($id) {
		$data = array(
			'name' => $this->input->post('name'),
			'position' => $this->input->post('position'),
			'mobile' => $this->input->post('mobile'),
			'tel' => $this->input->post('tel'),
			'tax' => $this->input->post('tax'),
			'email' => $this->input->post('email')			
		);
		
		$this->db->where('id', $id);
		$this->db->update('contact_person', $data);
	}
	
	//删除联系人
	public function del_by_id ($id) {
		$this->db->delete('contact_person', array('id' => $id));
		
	}
}